﻿using Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entities.QueryPersist
{
    public class EFQueryRepository : IQueryStore
    {
        IEnumerable<TEntity> IQueryStore.Read<TEntity>()
        {
            return db.Set<TEntity>().ToList();  
        }

        IEnumerable<TEntity> IQueryStore.Read<TEntity>(Func<TEntity, bool> selector)
        {
            return db.Set<TEntity>().Where(selector).ToList();
        }

        TEntity IQueryStore.Read<TEntity>(int id)
        {
            return db.Set<TEntity>().Find(id);
        }

        private QueryDbContext db = new QueryDbContext();

        #region Implement IDisposable
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    db.Dispose();
                }

                disposedValue = true;
            }
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
            Dispose(true);
        }
        #endregion
    }
}
